// unused code


using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
//using System.Windows.Forms;
using System.Drawing.Drawing2D;



namespace EForms
{
	


	/// <summary>
	/// Summary description for Control1.
	/// </summary>
	[System.ComponentModel.DesignerCategory("None")]
	[System.Drawing.ToolboxBitmap(typeof(System.Windows.Forms.Button))]
	public class ButtonXP : System.Windows.Forms.Control, System.Windows.Forms.IButtonControl
	{
		/// <summary>
		/// This variable stores a reference to our renderer object.
		/// </summary>
		private EForms.Themes.ButtonRenderer Renderer = null;
	
		protected Themes.States			state = Themes.States.Normal;
		protected GraphicsPath		path;
		protected ToolTip			toolTip;
		private Rectangle			bounds;
		private Image				image;
		protected SolidBrush			textBrush;
		
		
		private Point				imagePoint, textPoint;
		protected bool				isDefault = false;
		private System.Windows.Forms.DialogResult dialogResult = System.Windows.Forms.DialogResult.None;

		protected Color			    textColor = SystemColors.ControlText;

		public System.Windows.Forms.DialogResult DialogResult 
		{
			get
			{
				return this.dialogResult;
			}
			set
			{
				this.dialogResult = value;
			}
		}
	


		public ButtonXP()
		{
			Renderer = new Themes.ButtonRenderer();

			this.SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer, true);
			this.SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, true);
			this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
			this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
			this.SetStyle(System.Windows.Forms.ControlStyles.StandardDoubleClick, false);
			this.SetStyle(System.Windows.Forms.ControlStyles.Selectable, true);
			
			this.ResizeRedraw = true;
			this.toolTip = new ToolTip();
		
		}

		public bool IsDefault
		{
			get
			{
				return this.isDefault;
			}
			
		}
		public Themes.States State
		{
			get
			{
				return this.state;
			}
		}
		public System.Drawing.Brush TextBrush
		{
			get 
			{ 
				return this.textBrush; 
			}
		
		}

		/// <summary>
		/// We override this method to allow possibly our own renderer to paint 
		/// the background of the control.</summary>
		protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
		{
			if(null != Renderer)
			{
				Renderer.PaintBackground(pevent,this);
			}
			else
			{
				base.OnPaintBackground(pevent);
			}
		}

	

		/// <summary>
		/// We override this method to allow possibly our own renderer to paint 
		/// the control and therefore override the .NET painting.</summary>
		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		{
			if(null != Renderer)
			{
				CreateTextBrushes();
				Renderer.Paint(e,this);
				
				e.Graphics.DrawString(this.Text, this.Font , this.TextBrush, this.TextPoint, Renderer.StringFormat);

			}
			else
			{
				base.OnPaint(e);
			}
		}
	

	
		protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
		{
			if ((e.Button & System.Windows.Forms.MouseButtons.Left) != System.Windows.Forms.MouseButtons.Left) 
				return;

			if (bounds.Contains(e.X, e.Y))
			{
				state = Themes.States.Pushed;
				this.Focus();
			} 
			else state = Themes.States.Normal;
			this.Invalidate(bounds);
			base.OnMouseDown(e);
		}

		protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
		{
			if ((e.Button & System.Windows.Forms.MouseButtons.Left) == System.Windows.Forms.MouseButtons.Left)
				state = Themes.States.Normal;
			this.Invalidate(bounds);
			base.OnMouseUp(e);
		}

		protected override void OnEnter(System.EventArgs e)
		{
			this.Invalidate(bounds);
			base.OnEnter(e);
		}

		protected override void OnLeave(System.EventArgs e)
		{
			this.Invalidate(bounds);
			base.OnLeave(e);
		}
		protected override void OnMouseEnter(EventArgs e)
		{
			state = Themes.States.MouseOver;
			this.Invalidate(bounds);
			base.OnMouseEnter(e);
		}
		protected override void OnMouseLeave(EventArgs e)
		{
			state = Themes.States.Normal;
			this.Invalidate();
			base.OnMouseLeave(e);
		}


		protected void CreateRegion()
		{
			this.Renderer.CreateRegion((System.Windows.Forms.Control)this);

		}
		protected override void OnParentChanged(EventArgs e)
		{
			if (Parent == null) 
				return;
			SetPointsForTextWriting();
			Renderer.CreateRegion((System.Windows.Forms.Control) this);
			base.OnParentChanged(e);
		}
		protected override void OnSizeChanged(System.EventArgs e)
		{
			bounds = new Rectangle(0, 0, this.Width, this.Height);
			OnParentChanged(e);
			base.OnSizeChanged(e);
		}
		protected override void OnTextChanged(EventArgs e) 
		{
			Renderer.ResetStringFormat();
			CreateTextBrushes();

			SetPointsForTextWriting();
			base.OnTextChanged(e);
		}

		[DefaultValue(null),
		System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
		public Image Image
		{
			get	{ return image; }
			set
			{
				image = value;
				this.Invalidate();
			}
		}
		private void SetPointsForTextWriting()
		{
			int X = this.Width, Y = this.Height;

			if (Image != null) 
			{
				if (Text.Length == 0)
				{
					imagePoint = new Point((X - Image.Width)/2, (Y - Image.Height)/2); 
				}
				else
				{
					imagePoint = new Point(Themes.Margins.LeftMargin, (Y - Image.Height)/2);
				}
				textPoint = new Point(Themes.Margins.LeftMargin + Image.Width + Themes.Margins.TextMargin, (Y - this.Font.Height)/2);
			}
			else 
			{
				Size size = TextUtil.GetTextSize(this.CreateGraphics(), Text.Replace("&",""), Font, new Size(X, Y));
				textPoint = new Point((X - size.Width - 2)/2, (Y - this.Font.Height)/2);					
			}	
		}

		private void CreateTextBrushes()
		{
			if (textBrush != null) textBrush.Dispose();
			textBrush = new SolidBrush(textColor);
		}
		private void DisposeTextBrushes()
		{
			if (textBrush != null) textBrush.Dispose();
		}

		#region IButtonControl

		public void PerformClick()
		{
			if (base.CanSelect)
			{
				OnClick(EventArgs.Empty);
			}
		}

		public void NotifyDefault(bool value)
		{
			this.isDefault = value;
			this.Invalidate();
		}

		#endregion

		#region text 
		public override string Text
		{
			get 
			{ 
				return base.Text; 
			}
			set
			{		
				base.Text = value;
				this.Invalidate();
			}
		}

		[DefaultValue(typeof(Color), "ControlText"), 
		System.ComponentModel.RefreshProperties(RefreshProperties.Repaint)]
		public Color TextColor
		{
			get 
			{ 
				return this.textColor; 
			}
			set
			{
				this.textColor = value;
				this.CreateTextBrushes();
				this.Invalidate();
			}
		}
		
		public System.Drawing.Point ImagePoint
		{
			get 
			{ 
				return imagePoint; 
			}
		
		}
		public System.Drawing.Point TextPoint
		{
			get 
			{ 
				return textPoint; 
			}
		
		}
	
	
	
		
		

		#endregion

	}
}
